
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.Collections.Generic;
using FlowLib.Containers;
using FlowLib.Utils.Convert;
using ClientExample.Comparers;

namespace ClientExample.Containers
{
    public class SearchResultGroup : FlowSortedList<SearchResultInfo>
    {
        public string ID
        {
            get;
            set;
        }

        public string DisplayName
        {
            get;
            set;
        }

        public string DisplaySize
        {
            get;
            set;
        }

        public long Size
        {
            get;
            set;
        }

        public string DisplayType
        {
            get;
            set;
        }

        public SearchResultGroup(string key)
        {
            ID = key;
            comparer = new SearchResultComparer();
            trigger = true;
            ItemAdded += new FlowSortedList<SearchResultInfo>.SingleChangedDelegate(SearchResultGroup_ItemAdded);
        }

        void SearchResultGroup_ItemAdded(int itemPos, SearchResultInfo item)
        {
            // We should make better logic here.
            #region Size
            if (DisplaySize == null)
            {
                General.BinaryPrefixes bp;
                DisplaySize = string.Format("{0} {1}", General.FormatBytes(item.Info.Size, out bp), bp);
                Size = item.Info.Size;
            }
            #endregion
            if (DisplayName == null)
                DisplayName = item.Info.Get(ContentInfo.VIRTUAL);
            #region Type
            if (DisplayType == null && item.Info.ContainsKey(ContentInfo.VIRTUAL))
            {
                string type = string.Empty;
                string file = item.Info.Get(ContentInfo.VIRTUAL);
                int pos = 0;
                if (file.EndsWith("/"))
                {
                    type = "Directory";
                }
                else if ((pos = file.LastIndexOf(".")) != -1)
                {
                    type = file.Substring(pos).ToLower();
                    switch (type)
                    {
                        case ".mpg":    // Movie
                        case ".mpeg":
                        case ".mkv":
                        case ".avi":
                        case ".wmv":
                        case ".flac":
                        case ".mov":
                        case ".divx":
                        case ".m2v":
                        case ".ogm":
                            // TODO Check if this is a Serie/Movie.
                            type = "Video"; break;
                        case ".mp3":    // Sound
                        case ".wma":
                        case "mp4a":
                            type = "Sound"; break;
                        case ".mid":    // Sound (Midi)
                            type = "Sound (Midi)"; break;
                        case ".wav":    // Sound (Wave)
                            type = "Sound (Wave)"; break;
                        case ".txt":    // Document
                        case ".pdf":
                        case ".doc":
                            type = "Document"; break;
                        case ".nfo":    // Release Information
                            type = "Release Information"; break;
                        case ".jpg":    // Image
                        case ".jpeg":
                        case ".png":
                        case ".bmp":
                        case ".gif":
                            type = "Image"; break;
                        case ".rar":    // Part of Archive
                            // TODO Check if this is a Serie/Movie.
                            type = "Part of Archive"; break;
                        case ".zip":    // Archive
                        case ".7z":
                            type = "Archive"; break;
                        case ".srt":    // Subtitles
                        case ".sub":
                            type = "Subtitles"; break;
                        case ".exe":    // Executable
                            type = "Executable"; break;
                        default:
                            bool knownType = false;
                            // Is this a part of a Archive?
                            if (type.StartsWith(".r"))
                            {
                                string tmp = type.Substring(2);
                                int number = 0;
                                if (int.TryParse(type.Substring(2), out number))
                                {
                                    // TODO Check if this is a Serie/Movie.
                                    type = "Part of Archive";
                                    knownType = true;
                                }
                            }

                            if (!knownType)
                                type = "Unknown (" + type + ")";
                            break;
                    }
                }
                DisplayType = type;
            }
            #endregion
        }
    }
}
